import Vue from 'vue'
import Router from 'vue-router'
import routeComponents from '@/router/map'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/admin/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/admin/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/admin/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },
  {
    path: '/admin/config',
    component: Layout,
    redirect: '/admin/config',
    children: [{
      path: '/admin/config',
      name: 'admin:config',
      component: () => import('@/views/admin/config'),
      meta: { title: '配置', icon: 'dashboard' }
    }]
  },

  {
    path: '/admin/menu',
    component: Layout,
    redirect: '/admin/menu',
    children: [{
      path: '/admin/menu',
      name: 'admin:menu',
      component: () => import('@/views/admin/menu'),
      meta: { title: '菜单', icon: 'tree' }
    }]
  },

  {
    path: '/admin/admin',
    component: Layout,
    redirect: '/admin/admin',
    children: [{
      path: '/admin/admin',
      name: 'admin:admin',
      component: () => import('@/views/admin/admin'),
      meta: { title: '管理员', icon: 'tree' }
    }]
  },

  {
    path: '/admin/role',
    component: Layout,
    redirect: '/admin/role',
    children: [{
      path: '/admin/role',
      name: 'admin:role',
      component: () => import('@/views/admin/role'),
      meta: { title: '角色', icon: 'tree' }
    }]
  },

  {
    path: '/admin/friendlink',
    component: Layout,
    redirect: '/admin/friendlink',
    children: [{
      path: '/admin/friendlink',
      name: 'admin:friendlink',
      component: () => import('@/views/admin/friendlink'),
      meta: { title: '友情链接', icon: 'friendlink' }
    }]
  },
  {
    path: '/admin/user',
    component: Layout,
    redirect: '/admin/user',
    children: [{
      path: '/admin/user',
      name: 'admin:user',
      component: () => import('@/views/admin/user'),
      meta: { title: '用户', icon: 'tree' }
    }]
  },


  {
    path: '/admin/content',
    component: Layout,
    redirect: '/admin/content',
    children: [
      {
        path: '/admin/advertising/team',
        name: 'admin:team',
        component: () => import('@/views/admin/advertising.vue'),
        meta: { title: '工作团队', icon: 'user-friends' }
      },
      {
        path: '/admin/goods',
        name: 'admin:goods',
        component: () => import('@/views/admin/goods/list.vue'),
        meta: { title: '产品展示', icon: 'product-hunt' }
      },
      {
        path: '/admin/article/news',
        name: 'admin:article:news',
        component: () => import('@/views/admin/article/list.vue'),
        meta: { title: '新闻活动', icon: 'arc' }
      },
      {
        path: '/admin/article/regulation',
        name: 'admin:article:regulation',
        component: () => import('@/views/admin/article/list.vue'),
        meta: { title: '政策法规', icon: 'arc' }
      },
      {
        path: '/admin/advertising_category',
        name: 'admin:advertising_category',
        component: () => import('@/views/admin/advertising_category'),
        meta: { title: '广告位管理', icon: 'ad' }
      },
      {
        path: '/admin/advertising',
        name: 'admin:advertising',
        component: () => import('@/views/admin/advertising'),
        meta: { title: '广告管理', icon: 'ad' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  mode: 'hash',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export function filterRouter(routes) {
  const res = []
  routes.forEach(route => {
    const tmp = { ...route }
    tmp.component = import('@/layout/index.vue')
    if (routeComponents.hasOwnProperty(tmp.component)) {
      tmp.component = routeComponents[tmp.component]
    }
    tmp.meta = { title: tmp.title, icon: tmp.icon ? tmp.icon : '' }
    if (tmp.children.length > 0) {
      tmp.children = filterRouter(tmp.children)
    }
    res.push(tmp)
  })
  return res
}

export default router
